package com.holyn.guofang.view.my;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.holyn.guofang.R;
import com.holyn.guofang.model.webhttp.HttpConfigure;
import com.holyn.guofang.model.webhttp.wo.AddCardOrderWo;
import com.holyn.guofang.model.webhttp.wo.AttachedCardDictionaryWo;
import com.holyn.guofang.model.webhttp.wo.CardDictionaryWo;
import com.holyn.guofang.model.webhttp.wo.CouponInfoWo;
import com.holyn.guofang.presenter.BaseView;
import com.holyn.guofang.presenter.my.MyContract;
import com.holyn.guofang.presenter.my.MyPresenter;
import com.holyn.guofang.utils.HtmlUtil;
import com.holyn.guofang.view.BaseActivity;
import com.holyn.guofang.viewcommon.dialog.PayWayDialog;
import com.holyn.guofang.viewcommon.popupwindow.PopupSingleSelectFromBottom;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Holyn on 2017/8/22.
 */

public class MyRechargeActivity extends BaseActivity implements BaseView, MyContract.RechargeView {
    private static final int REQUEST_ACTION_POST_LIST_CARD_DICTIONARY= 0x0001;
    private static final int REQUEST_ACTION_POST_LIST_ATTACHED_CARD= 0x0002;
    private static final int REQUEST_ACTION_POST_COUPON_INFO= 0x0003;
    private static final int REQUEST_ACTION_POST_ADD_CARD_ORDER= 0x0004;

    private static final int REQUEST_CODE_GO_INUPT_ADDRESS = 0x0001;

    @BindView(R.id.tv_money_name)
    TextView tvMoneyName;
    @BindView(R.id.ll_select_pay_type)
    LinearLayout llSelectPayType;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.rb_receive_clause)
    RadioButton rbReceiveClause;
    @BindView(R.id.tv_read_clause)
    TextView tvReadClause;
    @BindView(R.id.tv_set_address)
    TextView tvSetAddress;
    @BindView(R.id.btn_submit)
    Button btnSubmit;

    PopupSingleSelectFromBottom popupSelectMoneyName = null;

    Integer cardID;
    Integer attenedCardID;
    Integer attenedCardCount;
    Integer needCoupon;
    Integer payWay;
    String couponCardNum = "";//无用参数
    Integer couponCardID = -1;//无用参数
    String remark = "";

    MyContract.Presenter presenter;

    MyRechargeAdapter2 rechargeAdapter;
    List<MyRechargeSection> myRechargeSectionList = new ArrayList<>();

    private PayWayDialog payWayDialog = null;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_recharge;
    }

    @Override
    protected void onMyCreate() {
        ButterKnife.bind(this);
        showToolbar("购卡充值");

        presenter = new MyPresenter(this);
        presenter.addRechargeView(this);
        presenter.postListCardDictionary(REQUEST_ACTION_POST_LIST_CARD_DICTIONARY, 1, -1, -1);

        rechargeAdapter = new MyRechargeAdapter2(MyRechargeActivity.this, myRechargeSectionList);
        rvList.setLayoutManager(new GridLayoutManager(MyRechargeActivity.this, 2));
        rvList.setAdapter(rechargeAdapter);
        rechargeAdapter.setOnGetAttacheClickListener(new MyRechargeAdapter2.OnGetAttacheClickListener() {
            @Override
            public void onGetAttacheClick(Integer parentID) {
                presenter.postListAttachedCardDictionary(REQUEST_ACTION_POST_LIST_ATTACHED_CARD, parentID);
            }
        });
        rechargeAdapter.setOnGetCouponClickListener(new MyRechargeAdapter2.OnGetCouponClickListener() {
            @Override
            public void onGetCouponClick(Integer type, Integer cardCount) {
                presenter.postCouponInfo(REQUEST_ACTION_POST_COUPON_INFO, type, cardCount);
            }
        });

        tvReadClause.setText(HtmlUtil.fromHtml("<u>"+"《使用条款》"+"</u>"));
        tvSetAddress.setText(HtmlUtil.fromHtml("<u>"+"邮寄附属卡的地址"+"</u>"));

    }

    @OnClick({R.id.ll_select_pay_type, R.id.tv_read_clause, R.id.tv_set_address, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_select_pay_type:
                showPopupSelectMoneyName();
                break;
            case R.id.tv_read_clause:
                break;
            case R.id.tv_set_address:
                MyRechargeAddressActivity.start(MyRechargeActivity.this, REQUEST_CODE_GO_INUPT_ADDRESS);
                break;
            case R.id.btn_submit:
                if (!rechargeAdapter.isSelectCard()){
                    toastMsg("主卡或附属卡最少要选一个");
                    return;
                }
                showPayWayDialog();
                break;
        }
    }

    private void showPopupSelectMoneyName (){
        if (popupSelectMoneyName == null){
            List<String> moneyNameList = new ArrayList<>();
            moneyNameList.add("人民币");
            popupSelectMoneyName =  new PopupSingleSelectFromBottom(MyRechargeActivity.this, "选择", moneyNameList);
            popupSelectMoneyName.setOnItemClickListener(new PopupSingleSelectFromBottom.OnItemClickListener() {
                @Override
                public void onItemClick(String item, int position) {
                    tvMoneyName.setText(item);
                }
            });
        }
        popupSelectMoneyName.showPopupWindow();
    }

    private void showPayWayDialog(){
        if (payWayDialog == null){
            payWayDialog = new PayWayDialog(MyRechargeActivity.this, new PayWayDialog.OnSureClickListener() {
                @Override
                public void onSureClick(Integer payWay) {
                    MyRechargeActivity.this.payWay = payWay;
                    submitCardOrder();
                }
            });
        }
        payWayDialog.show();
    }

    private void submitCardOrder(){
        cardID = rechargeAdapter.getCardID();
        attenedCardID = rechargeAdapter.getAttenedCardID();
        attenedCardCount = rechargeAdapter.getAttenedCardCount();
        needCoupon = rechargeAdapter.getNeedCoupon();
        presenter.postAddCardOrder(REQUEST_ACTION_POST_ADD_CARD_ORDER, cardID, attenedCardID, attenedCardCount, needCoupon, payWay, couponCardNum, couponCardID, remark);
    }

    @Override
    public void requestStart(Integer requestAction) {
        showLoadingDialog();
    }

    @Override
    public void requestError(Integer requestAction, Throwable e) {
        toastMsg("出现异常");
        closeLoadingDialog();
    }

    @Override
    public void requestComplete(Integer requestAction) {
        closeLoadingDialog();
    }

    @Override
    public void responseSuccess(int requestAction, CardDictionaryWo cardDictionaryWo) {
        if (cardDictionaryWo.getStatus() == HttpConfigure.STATUS_SUCCESS) {
            myRechargeSectionList.clear();
            myRechargeSectionList.addAll(getSectionList(cardDictionaryWo));
            rechargeAdapter.notifyDataSetChanged();

        } else {
            toastMsg("获取国方卡失败");
        }
    }

    @Override
    public void responseSuccess(int requestAction, AttachedCardDictionaryWo attachedCardDictionaryWo) {
        if (attachedCardDictionaryWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            rechargeAdapter.addAttachedCardData(attachedCardDictionaryWo);
        } else {
            toastMsg("获取附属卡失败");
        }
    }

    @Override
    public void responseSuccess(int requestAction, CouponInfoWo couponInfoWo) {
        if (couponInfoWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            rechargeAdapter.addCouponInfoData(couponInfoWo);
        } else {
            toastMsg("获取优惠卡失败");
        }
    }

    @Override
    public void responseSuccess(int requestAction, AddCardOrderWo addCardOrderWo) {
        if (addCardOrderWo.getStatus() == HttpConfigure.STATUS_SUCCESS){
            MyOrderDetailActivity.startForResult(MyRechargeActivity.this, "MyRechargeActivity", addCardOrderWo.getOrderID(), -1);
        } else {
            toastMsg("购卡下单失败");
        }
    }

    private List<MyRechargeSection> getSectionList(CardDictionaryWo cardDictionaryWo) {
        List<MyRechargeSection> myRechargeSectionList = new ArrayList<>();
        List<MyRechargeSection> dataBeanList1 = new ArrayList<>();//次卡
        List<MyRechargeSection> dataBeanList2 = new ArrayList<>();//时卡（期卡）
        List<CardDictionaryWo.DataBean> dataBeanList = cardDictionaryWo.getData();
        for (CardDictionaryWo.DataBean dataBean : dataBeanList) {
            if (dataBean.getCardType() == 1) {
                dataBeanList1.add(new MyRechargeSection(dataBean));
            } else if (dataBean.getCardType() == 2) {
                dataBeanList2.add(new MyRechargeSection(dataBean));
            }
        }
        myRechargeSectionList.add(new MyRechargeSection(true, "期卡：", false));
        myRechargeSectionList.addAll(dataBeanList2);
        myRechargeSectionList.add(new MyRechargeSection(true, "次卡：", true));
        myRechargeSectionList.addAll(dataBeanList1);

//        myRechargeSectionList.get(1).setCheck(true);
        return myRechargeSectionList;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode == RESULT_OK){
            switch (requestCode){
                case REQUEST_CODE_GO_INUPT_ADDRESS:
                    remark = data.getStringExtra(MyRechargeAddressActivity.INTENT_KEY_ADDRESS_ALL);
                    break;
            }
        }
    }
}
